package com.eascs.spider.yuqing.common.util;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * 
 * @author hzp
 *
 */
public class RedisUtil {

	private static JedisPool pool = null;
    
    /**
     * 构建redis连接池
     * 
     * @param ip
     * @param port
     * @return JedisPool
     */
    private static JedisPool getPool() {
        if (pool == null) {
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxTotal(1000);
            config.setMaxIdle(5);
            config.setMaxWaitMillis(1000*100);
            config.setTestOnBorrow(true);
            pool = new JedisPool(config, PropertiesUtil.getConfig("redis.ip"), 
            		Integer.valueOf(PropertiesUtil.getConfig("redis.port")), 1000*100, PropertiesUtil.getConfig("redis.password"));
        }
        return pool;
    }
    
    /**
     * 返还到连接池
     * 
     * @param pool 
     * @param redis
     */
	public static void returnResource(JedisPool pool, Jedis redis) {
        if (redis != null) {
            pool.returnResource(redis);
        }
    }
    
    /**
     * 获取数据
     * 
     * @param key
     * @return
     */
    private static String get(String key){
        String value = null;
        
        JedisPool pool = null;
        Jedis jedis = null;
        try {
            pool = getPool();
            jedis = pool.getResource();
            value = jedis.get(key);
        } catch (Exception e) {
            //释放redis对象
            pool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            //返还到连接池
            returnResource(pool, jedis);
        }
        
        return value;
    }
    
    private static Object get(byte[] key){
        byte[] value = null;
        
        JedisPool pool = null;
        Jedis jedis = null;
        try {
            pool = getPool();
            jedis = pool.getResource();
            value = jedis.get(key);
        } catch (Exception e) {
            //释放redis对象
            pool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            //返还到连接池
            returnResource(pool, jedis);
        }
        
        return value == null?null:ObjectUtil.deserialize(value);
    }
    
    //设置数据
    private static void set(String key , String value){
    	 JedisPool pool = null;
         Jedis jedis = null;
         try {
             pool = getPool();
             jedis = pool.getResource();
             jedis.set(key, value);
         } catch (Exception e) {
             //释放redis对象
             pool.returnBrokenResource(jedis);
             e.printStackTrace();
         } finally {
             //返还到连接池
             returnResource(pool, jedis);
         }
    }
    
    private static void set(String key , Object value){
    	JedisPool pool = null;
        Jedis jedis = null;
        try {
            pool = getPool();
            jedis = pool.getResource();
            jedis.set(key.getBytes(), ObjectUtil.serialize(value));
        } catch (Exception e) {
            //释放redis对象
            pool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            //返还到连接池
            returnResource(pool, jedis);
        }
   }
    
    private synchronized static boolean exist(String key){
    	JedisPool pool = null;
        Jedis jedis = null;
        try {
            pool = getPool();
            jedis = pool.getResource();
             return jedis.exists(key);
        } catch (Exception e) {
            //释放redis对象
            pool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            //返还到连接池
            returnResource(pool, jedis);
        }
        return false;
    }
    
    //设置或覆盖布隆去重器
    public static void setBloomFilter(Object object){
    	set("yuqing_bulong", object);
    }
    
    //初始化布隆去重器,如果是第一次则返回对象为null
    public static Object initBloomFilter(){
    	Object object = get("yuqing_bulong".getBytes());
    	return object;
    }
    
  
}
